package jieduan1_part4;

/**
 * 编写一个员工类   (1)属性: 姓名,年龄       如果年龄小于18龄大于150抛出年龄异常       如果姓名为null或者""抛出空异常
 */
public class Worker {
    private int age;
    private String name;

    public Worker() {
    }

    public Worker(int age, String name) {
        setAge(age);
        setName(name);
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age > 150 || age <18){
            try {
                throw new AgeException("年龄不合理");
            } catch (AgeException e) {
                e.printStackTrace();
            }
        }else {
            this.age = age;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if ( null == name || name.equals("") ){
            try {
                throw new NameException("姓名为空");
            } catch (NameException e) {
                e.printStackTrace();
            }
        }else{
            this.name = name;
        }
    }

    @Override
    public String toString() {
        return "Worker{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
